package com.logicwin.aibala;

import com.logicwin.aibala.services.IScreenService;
import com.logicwin.aibala.services.file.FileConnection;
import com.logicwin.aibala.services.impl.ServiceManager;
import com.logicwin.aibala.ui.screens.Downloads.ScreenDownloads;
import com.logicwin.aibala.ui.screens.Search.ScreenSearch;
import com.logicwin.aibala.ui.screens.screenmain.ScreenHome;

import android.app.ActivityGroup;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

import com.logicwin.aibala.com.AibalaException;
import com.logicwin.aibala.com.GlobalConfig;
import com.logicwin.aibala.model.androidSWInfo.AndroidSWInfoService;;
public class Main  extends ActivityGroup{
	
	public static final int ACTION_NONE = 0;	
	private static String TAG = Main.class.getCanonicalName();
	private final IScreenService screenService;
	
	private ImageButton mButtonRefresh;
	private ImageButton mButtonSearch;
	private ImageButton mButtonDownload;
	private ImageButton mButtonConfig;
	private ImageButton mButtonChat;
	
    public Main()
    {
    	super();
    	Log.i("Main", "start");
    	// Sets main activity (should be done before starting services)
    	ServiceManager.setMainActivity(this);    

    	// Gets services to avoid calling ServiceManager.get*() each time we want one
    	this.screenService = ServiceManager.getScreenService();	
    }	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // remove title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        if(!ServiceManager.start()){
        	Log.e(Main.TAG, "Failed to start services");
        	return; // Should exit
        }
        GlobalConfig.init();
        try {
			AndroidSWInfoService.init(this);
		} catch (AibalaException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        this.screenService.show(ScreenHome.class);
        initButtons();
    }
    private void initButtons(){
    	mButtonRefresh  = (ImageButton)findViewById(R.id.button_refresh); 
    	mButtonSearch   = (ImageButton)findViewById(R.id.button_search);
    	mButtonDownload = (ImageButton)findViewById(R.id.button_download);
    	mButtonConfig   = (ImageButton)findViewById(R.id.button_config);
    	mButtonChat		= (ImageButton)findViewById(R.id.button_chat);
    	
    	mButtonRefresh.setOnClickListener(refresh_OnClickListener);
    	mButtonSearch.setOnClickListener(search_OnClickListener);
    	mButtonDownload.setOnClickListener(download_OnClickListener);
    	mButtonConfig.setOnClickListener(config_OnClickListener);  
    	mButtonChat.setOnClickListener(chat_OnClickListener);    	
    }
	//@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		if(this.screenService.getCurrentScreen().haveMenu()){
			return this.screenService.getCurrentScreen().createOptionsMenu(menu);
		}
		
		return false;
	}

	//@Override
	public boolean onPrepareOptionsMenu(Menu menu){
		if(this.screenService.getCurrentScreen().haveMenu()){
			menu.clear();
			return this.screenService.getCurrentScreen().createOptionsMenu(menu);
		}
		return false;
	}
	public boolean onKeyDown(int keyCode, KeyEvent event){
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			ServiceManager.getScreenService().back();
			return true;
		}		
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return this.screenService.getCurrentScreen().onOptionsItemSelected(item);
	}	
	
	private OnClickListener refresh_OnClickListener = new OnClickListener() {
		public void onClick(View v) {
			Log.i("Main","refresh_OnClickListener");
		}
	};
	private OnClickListener search_OnClickListener = new OnClickListener() {
		public void onClick(View v) {
			ServiceManager.getScreenService().show(ScreenSearch.class);
			Log.i("Main","search_OnClickListener");
		}
	};	
	private OnClickListener download_OnClickListener = new OnClickListener() {
		public void onClick(View v) {
			ServiceManager.getScreenService().show(ScreenDownloads.class);
			Log.i("Main","download_OnClickListener");
		}
	};	
	private OnClickListener config_OnClickListener = new OnClickListener() {
		public void onClick(View v) {
			Log.i("Main","config_OnClickListener");
		}
	};	
	private OnClickListener chat_OnClickListener = new OnClickListener() {
		public void onClick(View v) {
			Log.i("Main","chat_OnClickListener");
		}
	};	
	
}
